import { TargetState } from "framework/FSM/DecisionTree/DecisionTargetState";
import { DecisionIsPowerWorkable } from "../../Control/Decision/powerCreep/DecisionIsPowerWorkable";
import { DecisionIsTaskMode } from "../../Control/Decision/powerCreep/DecisionIsTaskMode";
import { State_Waitting } from "../../Control/State/powerCreep/State_Waitting";
import { HSM_PowerCreepActive } from "../../Control/State/powerCreep/HSM_PowerCreepActive";
import { DecisionTreeTransition } from "framework/FSM/Transition/DecisionTreeTransition";
import { State_GetOPS } from "../../Control/State/powerCreep/State_GetOPS";
import { SubMachineState } from "framework/FSM/SubMachineState";
import { DecisionIsOPSReady } from "../../Control/Decision/powerCreep/DecisionIsOPSReady";
import { State_UserPower } from "../../Control/State/powerCreep/State_UserPower";
import { State_EnbaleRoom } from "../../Control/State/powerCreep/State_EnbaleRoom";
import { State_PCRenew } from "../../Control/State/powerCreep/State_Renew";
import { State_Spawn } from "../../Control/State/powerCreep/State_Spawn";
import { DecisionIsDead } from "../../Control/Decision/powerCreep/DecisionIsDead";
import { DecisionIsLowerTTL } from "../../Control/Decision/powerCreep/DecisionIsLowerTTL";
import { DecisionRoomPowerEnable } from "../../Control/Decision/powerCreep/DecisionRoomPowerEnable";

export class FSMPowerCreep
{
    public static Create()
    {
        const state_preapre =this.CreatePrepare();
        const state_waitting = new State_Waitting();
        const state_work = this.CreateWork();
        const D_prepare = new DecisionIsPowerWorkable(
            new DecisionIsTaskMode(
                new TargetState   (state_work),
                new TargetState   (state_waitting))
            ,undefined);
        const D_wokrer = new DecisionIsPowerWorkable(
                new DecisionIsTaskMode(
                    undefined,
                    new TargetState   (state_waitting))
            ,new TargetState(state_preapre));

        const D_Waitting = new DecisionIsPowerWorkable(
            new DecisionIsTaskMode(
                new TargetState   (state_work),
               undefined)
        ,new TargetState(state_preapre));
        const pc = new HSM_PowerCreepActive([state_preapre,state_work
        ,state_waitting]);
        state_preapre.addTransition(new DecisionTreeTransition(D_prepare));
        state_work.addTransition(new DecisionTreeTransition(D_wokrer));
        state_waitting.addTransition(new DecisionTreeTransition(D_Waitting));
        return pc;
    }

    private static CreateWork()
    {
        const state_userPower = this.CreateUsePower();
        const state_getops = new State_GetOPS();
        const SUB_Behavior = new SubMachineState([state_userPower,state_getops]);
        const d_getops = new DecisionIsOPSReady(new TargetState(state_userPower),undefined);
        const D_userPower =  new DecisionIsOPSReady(undefined,new TargetState(state_getops));
        state_userPower.addTransition(new DecisionTreeTransition(D_userPower));
        state_getops.addTransition(new DecisionTreeTransition(d_getops));
        return SUB_Behavior;
    }

    private static CreateUsePower()
    {
        return new State_UserPower();
    }

    // 前置的准备工作,孵化,enbaleroom,renew
    private static CreatePrepare()
    {
        const state_enableroom = new State_EnbaleRoom();
        const state_renew = new State_PCRenew();
        const state_spawn = new State_Spawn();
        const SUB_Behavior = new SubMachineState([state_enableroom,state_renew,state_spawn]);

        const D_spawn = new DecisionIsDead(
            undefined,
            new DecisionIsLowerTTL(
                new TargetState(state_renew),
                new DecisionRoomPowerEnable(
                    undefined,
                    new TargetState(state_enableroom))
            )
            );
        const D_renew = new DecisionIsDead(
            new TargetState(state_spawn),
            new DecisionIsLowerTTL(
                undefined,
                new DecisionRoomPowerEnable(
                    undefined,
                    new TargetState(state_enableroom))
            )
            );

        const D_enableroom = new DecisionIsDead(
            new TargetState(state_spawn),
            new DecisionIsLowerTTL(
                new TargetState(state_renew)
            )
            );
        state_enableroom.addTransition(new DecisionTreeTransition(D_enableroom));
        state_renew.addTransition(new DecisionTreeTransition(D_renew));
        state_spawn.addTransition(new DecisionTreeTransition(D_spawn));
        return SUB_Behavior;
    }
}